﻿using MySql.Data.MySqlClient;
using Server.CustomObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Server.DataBase
{
    class DBSession : Server.Interfaces.IDataBaseSession
    {
        MySqlConnection mConnection;

        public DBSession(MySqlConnection xConnection)
        {
            this.mConnection = xConnection;
        }

        public Session createSession(int userID)
        {
            String cmdText = "";
            MySqlCommand cmd = null;
            Session _Session = null;
            int _SessionId;
            try
            {

                cmdText = "INSERT INTO " + Globals.Tables.SESSION + " (" + Globals.SessionFields.ID + ") VALUE (NULL)";
                cmd = new MySqlCommand(cmdText, mConnection);
                cmd.ExecuteNonQuery();
                _SessionId = Convert.ToInt32(cmd.LastInsertedId);

                cmdText = "INSERT INTO " + Globals.Tables.CUSERSESSION + " (" + Globals.ConUserSessionFields.ID + "," +
                            Globals.ConUserSessionFields.USERID + "," + Globals.ConUserSessionFields.SESSIONID + ")" +
                            "VALUE (NULL,?userID,?sessionID)";

                cmd = new MySqlCommand(cmdText, mConnection);
                cmd.Parameters.Add("@userID", MySqlDbType.Int32).Value = userID;
                cmd.Parameters.Add("@sessionID", MySqlDbType.Int32).Value = _SessionId;

                cmd.ExecuteNonQuery();

                if (_SessionId <= 0) return null;
                _Session = new Session(_SessionId);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                cmdText = null;
                cmd = null;
            }
            return _Session;
        }
        public Boolean connectUserToSession(int userId, int sessionId)
        {
            String cmdText = "";
            MySqlCommand cmd = null;
            try
            {
                cmdText = "INSERT INTO " + Globals.Tables.CUSERSESSION + " (" + Globals.ConUserSessionFields.ID + "," +
                            Globals.ConUserSessionFields.USERID + "," + Globals.ConUserSessionFields.SESSIONID + ")" +
                            "VALUE (NULL,?userID,?sessionID)";

                cmd = new MySqlCommand(cmdText, mConnection);
                cmd.Parameters.Add("@userID", MySqlDbType.Int32).Value = userId;
                cmd.Parameters.Add("@sessionID", MySqlDbType.Int32).Value = sessionId;
                return Convert.ToBoolean(cmd.ExecuteScalar());
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                cmdText = null;
                cmd = null;
            }

        }
        public Boolean removeUserFromSession(int userId, int sessionId)
        {
            return false;
        }
        public Boolean closeSession(int userId, int sessionId)
        {

            return false;
        }

        public long Count()
        {
            String cmdText = "SELECT COUNT( " + Globals.SessionFields.ID + ") FROM " + Globals.Tables.SESSION;

            MySqlCommand cmd = new MySqlCommand(cmdText, mConnection);

            return Convert.ToInt64(cmd.ExecuteScalar());
        }
    }
}
